package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.EDiscountInfo;
import com.ruoyi.system.domain.EDiscountLog;
import com.ruoyi.system.domain.EPerfectLog;
import com.ruoyi.system.service.IEDiscountInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.EDiscountInfoLogMapper;
import com.ruoyi.system.domain.EDiscountInfoLog;
import com.ruoyi.system.service.IEDiscountInfoLogService;

/**
 * 贴现人信息完善记录Service业务层处理
 *
 * @author ruoyi
 * @date 2022-04-21
 */
@Service
public class EDiscountInfoLogServiceImpl implements IEDiscountInfoLogService
{
    @Autowired
    private EDiscountInfoLogMapper eDiscountInfoLogMapper;
    @Autowired
    IEDiscountInfoService ieDiscountInfoService;

    /**
     * 查询贴现人信息完善记录
     *
     * @param id 贴现人信息完善记录主键
     * @return 贴现人信息完善记录
     */
    @Override
    public EDiscountInfoLog selectEDiscountInfoLogById(Long id)
    {
        return eDiscountInfoLogMapper.selectEDiscountInfoLogById(id);
    }

    /**
     * 查询贴现人信息完善记录列表
     *
     * @param eDiscountInfoLog 贴现人信息完善记录
     * @return 贴现人信息完善记录
     */
    @Override
    @DataScope()
    public List<EDiscountInfoLog> selectEDiscountInfoLogList(EDiscountInfoLog eDiscountInfoLog)
    {
        return eDiscountInfoLogMapper.selectEDiscountInfoLogList(eDiscountInfoLog);
    }

    /**
     * 新增贴现人信息完善记录
     *
     * @param eDiscountInfoLog 贴现人信息完善记录
     * @return 结果
     */
    @Override
    public int insertEDiscountInfoLog(EDiscountInfoLog eDiscountInfoLog)
    {
        eDiscountInfoLog.setCreateTime(DateUtils.getNowDate());
        return eDiscountInfoLogMapper.insertEDiscountInfoLog(eDiscountInfoLog);
    }

    /**
     * 修改贴现人信息完善记录
     *
     * @param eDiscountInfoLog 贴现人信息完善记录
     * @return 结果
     */
    @Override
    public int updateEDiscountInfoLog(EDiscountInfoLog eDiscountInfoLog)
    {
        return eDiscountInfoLogMapper.updateEDiscountInfoLog(eDiscountInfoLog);
    }

    /**
     * 批量删除贴现人信息完善记录
     *
     * @param ids 需要删除的贴现人信息完善记录主键
     * @return 结果
     */
    @Override
    public int deleteEDiscountInfoLogByIds(Long[] ids)
    {
        return eDiscountInfoLogMapper.deleteEDiscountInfoLogByIds(ids);
    }

    /**
     * 删除贴现人信息完善记录信息
     *
     * @param id 贴现人信息完善记录主键
     * @return 结果
     */
    @Override
    public int deleteEDiscountInfoLogById(Long id)
    {
        return eDiscountInfoLogMapper.deleteEDiscountInfoLogById(id);
    }

    @Override
    public void importEDiscountInfoLog(Long discountId, List<EDiscountInfoLog> eDiscountInfoLogs, String operName, LoginUser loginUser) {
        for(EDiscountInfoLog eDiscountInfoLog:eDiscountInfoLogs){
            if(StringUtils.isNotEmpty(eDiscountInfoLog.getTicketCode())){
                eDiscountInfoLog.setDiscountId(discountId);
                eDiscountInfoLog.setCreateBy(operName);
                eDiscountInfoLog.setUserId(loginUser.getUserId());
                eDiscountInfoLog.setDeptId(loginUser.getDeptId());
                eDiscountInfoLog.setPerfectState("UNFINISHED");
                insertEDiscountInfoLog(eDiscountInfoLog);
            }
        }
        allEDiscountInfoLog(discountId,loginUser);
    }

    @Override
    public Boolean isUnfinished(Long discountId) {
        Long num = eDiscountInfoLogMapper.isUnfinishedByDiscountId(discountId);
        if(num>0){
            return true;
        }
        return false;
    }

    public void allEDiscountInfoLog(Long discountId,LoginUser loginUser){
        EDiscountInfoLog eDiscountInfoLog=new EDiscountInfoLog();
        eDiscountInfoLog.setDiscountId(discountId);
        List<EDiscountInfoLog> eDiscountInfoLogs=selectEDiscountInfoLogList(eDiscountInfoLog);
        for(EDiscountInfoLog discountInfoLog:eDiscountInfoLogs){
            /**完善贴现人名称*/
            setDiscountApplicantName(discountInfoLog,loginUser);
        }
    }

    private void setDiscountApplicantName(EDiscountInfoLog discountInfoLog, LoginUser loginUser) {
        EDiscountInfo eDiscountInfo=new EDiscountInfo();
        eDiscountInfo.setTicketCode(discountInfoLog.getTicketCode());
        List<EDiscountInfo> eDiscountInfos= ieDiscountInfoService.selectEDiscountInfoList(eDiscountInfo);
        if(eDiscountInfos.size()>0){
            discountInfoLog.setDiscountApplicantName(eDiscountInfos.get(0).getDiscountApplicantName());
            discountInfoLog.setPerfectState("COMPLETED");
            updateEDiscountInfoLog(discountInfoLog);
        }
    }
}
